package net.gazhi.delonix.rbac.web.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import net.gazhi.delonix.core.annonation.ApiAction;
import net.gazhi.delonix.core.annonation.ApiController;
import net.gazhi.delonix.core.annonation.ApiLevel;
import net.gazhi.delonix.core.web.AbstractAnnotationController;
import net.gazhi.delonix.rbac.entity.Role;
import net.gazhi.delonix.rbac.entity.User;
import net.gazhi.delonix.rbac.form.UserEditForm;
import net.gazhi.delonix.rbac.service.RoleService;
import net.gazhi.delonix.rbac.service.UserService;

@Controller
@RequestMapping("/rbac/user")
@ApiController("用户模块")
public class UserController extends AbstractAnnotationController {

	@Autowired
	private UserService userService;

	@Autowired
	private RoleService roleService;

	@ApiAction(name = "用户列表", level = ApiLevel.GROUP_ADMIN)
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list() {
		List<Role> roles = this.roleService.listAll();
		List<User> users = this.userService.list();
		return new ModelAndView().addObject("roles", roles).addObject("users", users);
	}

	@ApiAction(name = "保存用户", level = ApiLevel.GROUP_ADMIN)
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ModelAndView save(@RequestBody @Valid UserEditForm form, BindingResult result) {
		this.userService.saveOrUpdate(form, result);
		return super.resultView(result);
	}
	

}
